package org.dtrd.config.wechat.api;

import com.alibaba.fastjson.JSONObject;
import org.dtrd.config.wechat.entity.bean.*;
import org.dtrd.config.wechat.entity.bean.officialaccount.WechatMenu;
import org.dtrd.config.wechat.entity.bean.officialaccount.WechatOfficialAccountUserInfo;
import org.dtrd.config.wechat.entity.response.WechatResponse;
import org.dtrd.config.wechat.entity.response.WxCreateIndividuationResponse;
import org.dtrd.config.wechat.entity.response.WxTagListResponse;
import org.dtrd.config.wechat.entity.response.WxUnionidResp;

import java.util.List;

/**
 * 微信公众号相关接口
 *
 * @author xiao
 */
public interface IWechatOfficialAccountApi extends IWechatBaseApi {

    String TICKET = "TICKET";

    String API_TICKET_PATH = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";

    /**
     * 获取用户信息
     */
    String GET_USER_INFO = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

    /**
     * 获取unionid
     */
    String GET_UNIONID = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

    /**
     * 查询错误 rid 消息
     */
    String QUERY_RID = "https://api.weixin.qq.com/cgi-bin/openapi/rid/get?access_token=ACCESS_TOKEN";

    /**
     * https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#0
     * 公众号授权的 access_token
     */
    String WEB_PAGE_ACCESS_TOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";

    /**
     * https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#2
     * 刷新微信公众号的 access_token
     */
    String WEB_PAGE_REFRESH_TOKEN = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN";

    /**
     * https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#4
     * 校验网页授权的 ACCESS_TOKEN 是否有效
     */
    String WEB_PAGE_CHECK_TOKEN = "https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID";

    /**
     * 创建公众号菜单
     */
    String CREATE_COMMON_MENU = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";

    /**
     * 创建个性化菜单
     */
    String CREATE_CONDITIONAL_MENU = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=ACCESS_TOKEN";

    /**
     * 删除菜单, 在个性化菜单时，调用此接口会删除默认菜单及全部个性化菜单。
     */
    String REMOVE_MENU = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";

    /**
     * 查询公众号菜单
     */
    String QUERY_MENU = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";

    /**
     * 获取模板ID
     */
    String GETTEMPLATEID_PATH = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN";

    /**
     * 获取模板列表
     */
    String GETTEMPLATELIST_PATH = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN";

    /**
     * 发送模板消息
     */
    String SEND_TEMPLATE_PATH = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";

    /**
     * https://developers.weixin.qq.com/doc/offiaccount/User_Management/User_Tag_Management.html
     * 创建标签
     */
    String CREATE_TAG = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token=ACCESS_TOKEN";

    /**
     * 获取标签
     */
    String LIST_TAG = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token=ACCESS_TOKEN";

    /**
     * 绑定标签
     */
    String BINDING_TAG = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=ACCESS_TOKEN";

    /**
     * 查询用户标签
     */
    String QUERY_USER_TAG = "https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=ACCESS_TOKEN";


    /**
     * https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html
     * 创建微信二维码
     */
    String QR_CODE = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=ACCESS_TOKEN";

    /**
     * 根据 ticket 换取二维码图片
     */
    String GET_QR_CODE = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET";

    /**
     * 发送模板消息
     *
     * @param accessToken
     * @param body
     * @param <T>
     */
    default <T> void sendTemplateMsg(String accessToken, T body) {
        doPost(SEND_TEMPLATE_PATH, accessToken, body);
    }


    // region ================================  标签与菜单管理 ====================================================

    /**
     * 创建微信标签
     *
     * @param accessToken
     * @param tagName     标签名字
     * @return 标签对象
     */
    WechatCreateTagJSON createWechatTag(AccessToken accessToken, String tagName);

    /**
     * 获取标签列表
     */
    WxTagListResponse listWechatTag(AccessToken accessToken);

    /**
     * 批量绑定标签和用户
     *
     * @param openids openids
     * @param tagid   标签id
     * @return {@link WechatResponse}
     */
    WechatResponse bindingTag(AccessToken accessToken, List<String> openids, Integer tagid);

    /**
     * 查询用户标签
     *
     * @param accessToken
     * @param openid
     * @return
     */
    JSONObject queryUserTag(AccessToken accessToken, String openid);

    /**
     * 创建个性化菜单
     *
     * @param accessToken
     * @param menu
     * @return
     */
    WxCreateIndividuationResponse createConditionalMenu(AccessToken accessToken, WechatMenu menu);

    /**
     * 创建普通菜单
     *
     * @param accessToken
     * @param menu
     * @return
     */
    WechatResponse createCommonMenu(AccessToken accessToken, WechatMenu menu);

    /**
     * 删除菜单
     *
     * @param accessToken
     * @return
     */
    WechatResponse removeMenu(AccessToken accessToken);

    /**
     * 查询公众号菜单
     *
     * @param accessToken
     * @return
     */
    JSONObject queryOfficeAccountMenu(AccessToken accessToken);

    //endregion ================================  标签与菜单管理 ====================================================

    /**
     * 查看错误信息
     */
    String getRidInfo(AccessToken accessToken, WechatRidRequest request);

    /**
     * 获取登陆用户信息
     */
    WechatOfficialAccountUserInfo getUserInfo(String accessToken, String openid);

    /**
     * 获取unionid
     */
    WxUnionidResp getUnionid(String accessToken, String openid);

    /**
     * 校验 access_token 是否有效
     *
     * @param token  待校验的 access_token
     * @param openid openid
     * @return 校验结果
     */
    boolean checkOfficialAccountAccessToken(String token, String openid);

    /**
     * 获取网页授权的 access_token
     */
    OfficialAccountAccessToken getOfficialAccountAccessToken(String code);

    /**
     * 刷新 access_token
     */
    OfficialAccountAccessToken refreshOfficialAccountAccessToken(String refreshToken);

    /**
     * 校验微信回调签名
     *
     * @param signature
     * @param timestamp
     * @param nonce
     * @param token
     * @return
     */
    boolean checkSignature(String signature, String timestamp, String nonce, String token);

    ApiTicket getApiTicket(String accessToken);

    /**
     * 微信签名
     *
     * @param apiTicket
     * @param pageUrl
     * @param noncestr
     * @param timestamp
     * @return
     */
    String doWechatSign(String apiTicket, String pageUrl, String noncestr, long timestamp);

    // region ====================================== 模板消息与生成带参数二维码 ============================================

    /**
     * 查询模板消息列表
     *
     * @param accessToken
     * @return
     */
    String getTemplateList(String accessToken);

    /**
     * 生成带参数的二维码
     * http请求方式: POST
     * URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN
     * POST数据格式：json
     * {"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str":"test"}} }
     * 创建二维码
     *
     * @param content 二维码的内容
     * @return
     */
    WechatQrCodeTicket createQrCode(String accessToken, String sceneId , String content, Integer expireSeconds);

}
